package com.yanqu.road.server.cmd.activity.shuihuyezicard;

import com.yanqu.road.pb.activity.ShuiHuYeZiCardProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.shuihuyezicard.ShuiHuYeZiCardModule;
import com.yanqu.road.server.manger.activity.shuihuyezicard.ShuiHuYeZiCardMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

/**
 * @Description
 * @Author cwq
 * @Data 2021/12/3 16:03
 */
@Cmd(code = GameProtocol.S_SHUIHUYEZICARD_PLAY_CARD, desc = "水浒叶子牌 出牌")
public class ShuiHuYeZiCardPlayCardCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ShuiHuYeZiCardProto.ShuiHuYeZiCardPlayCardReqMsg reqMsg = ShuiHuYeZiCardProto.ShuiHuYeZiCardPlayCardReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        int type = reqMsg.getType();

        ret = ShuiHuYeZiCardMgr.canEntryActivity(player,activityId);
        if(ret != 0){
            return;
        }
        ShuiHuYeZiCardModule shuiHuYeZiCardModule = player.getModule(ShuiHuYeZiCardModule.class);
        if(type == 0){
            // 练习模式
            if(shuiHuYeZiCardModule.getExerciseRoom() == null){
                // 未开始
                ret = GameErrorCode.E_SHUIHUYEZICARD_ACTIVITY_NOT_OPEN_GAME;
                return;
            }
            shuiHuYeZiCardModule.getExerciseRoom().playCard(player,reqMsg.getId(),reqMsg.getRound());
        }else if(type == 1){
            // pve模式
            if(shuiHuYeZiCardModule.getNpcRoom() == null){
                // 未开始
                ret = GameErrorCode.E_SHUIHUYEZICARD_ACTIVITY_NOT_OPEN_GAME;
                return;
            }
            shuiHuYeZiCardModule.getNpcRoom().playCard(player,reqMsg.getId(),reqMsg.getRound());
        }else {
            // pvp 模式
            player.sendPacket(CrossProtocol.C_CROSS_SHUIHUYEZICARD_PLAY_CARD,reqMsg.toBuilder());
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            ShuiHuYeZiCardProto.ShuiHuYeZiCardPlayCardRespMsg.Builder resp = ShuiHuYeZiCardProto.ShuiHuYeZiCardPlayCardRespMsg.newBuilder();
            resp.setRet(ret);
            player.sendPacket(ClientProtocol.U_SHUIHUYEZICARD_PLAY_CARD,resp);
        }
    }
}
